package com.zerui.manager.app.openplan.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.cola.dto.PageResponse;
import com.zerui.manager.openplan.OpenPlan;
import com.zerui.manager.openplan.domainservice.OpenPlanRepository;
import com.zerui.manager.web.cmd.OpenPlanQryCmd;
import com.zerui.manager.web.vo.OpenPlanVO;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wh
 * @create_time 2023-09-02 11:09
 */
@Component
@CatchAndLog
public class OpenPlanQryCmdExe {

    @Resource
    private OpenPlanRepository openPlanRepository;

    public PageResponse<OpenPlanVO> execute(OpenPlanQryCmd openPlanQryCmd) {
        Page<OpenPlan> page = openPlanRepository.getPageList(openPlanQryCmd);
        List<OpenPlan> records = page.getRecords();
        List<OpenPlanVO> openPlanVOS = new ArrayList<>(16);
        if(CollectionUtils.isNotEmpty(records)){
            records.forEach(e-> openPlanVOS.add(OpenPlanVO.builder()
                    .name(e.getName())
                    .accept(e.getAccept())
                    .remark(e.getRemark())
                    .weekend(e.getWeekend())
                    .createTime(e.getCreateTime())
                    .id(e.getId())
                    .date(createDate(e.getStartDate(),e.getEndDate()))
                    .time(createTime(e.getStartTime(),e.getEndTime()))
                    .build()));
        }
        return PageResponse.of(openPlanVOS,(int)page.getTotal(),
                (int) page.getSize(),
                (int) page.getCurrent());
    }

    private String createTime(LocalTime startTime, LocalTime endTime) {
        StringBuilder builder = new StringBuilder();
        String start_time = startTime.format(DateTimeFormatter.ofPattern("HH:mm"));
        String end_time = endTime.format(DateTimeFormatter.ofPattern("HH:mm"));
        return builder.append(start_time).append("-").append(end_time).toString();
    }

    private String createDate(LocalDate startDate, LocalDate endDate) {
        StringBuilder builder = new StringBuilder();
        String start_date = startDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        String end_date = endDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        return builder.append(start_date).append("-").append(end_date).toString();
    }

}
